package com.xiaow.springsecuriydemo.exception;

import com.alibaba.fastjson.JSON;
import com.xiaow.springsecuriydemo.utils.WebUtils;
import com.xiaow.springsecuriydemo.vo.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName AccessDeniedHandlerImpl
 * @Author xiaow
 * @DATE 2022/9/16 19:28
 **/
@Component
//处理权限不足时的异常处理器
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
//处理异常
        Result result = Result.result("您的权限不足", 403, "");
        String s = JSON.toJSONString(result);
        WebUtils.renderString(httpServletResponse,s);
    }
}
